package com.hrt.demo.wsb.record;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


import java.util.Iterator;
import java.util.Map;






import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.util.WebParams;
import com.hrt.util.upload.Plupload;
import com.hrt.util.upload.UploadUtil;


@Controller
@RequestMapping("manage/demo/record")
public class RecordOnlineController {

	protected Logger logger=Logger.getLogger(RecordOnlineController.class);
	
	
	
	protected String FileDir = WebParams.WEB_RECORD_UPLOAD_FOLER;
	
	protected String subfix = WebParams.WEB_RECORD_SUBFIX;

	@Autowired
	private UploadUtil uploadUtil;

	@RequestMapping("/show")
	public ModelAndView show() {
		ModelAndView mv = new ModelAndView();
		// /HRTWeb/WebContent/
		mv.setViewName("manage/demo/wavRecord/show");
		return mv;
	}

	@ResponseBody 
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public String upload(HttpServletRequest request,
			HttpServletResponse response) {
		
		/**
		 * 获取项目主路径
		 */
		 String basePath=request.getSession().getServletContext().getRealPath("/");
		 
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;   
		// plupload.setRequest(multipartRequest);
		 //Plupload plupload,
		 Map<String, MultipartFile> map=multipartRequest.getFileMap();
		 System.err.println("hello world"+map.size());
		 
		 File folder=new File(basePath+"/"+FileDir);
			if(!folder.exists()){
				folder.mkdirs();
				folder.setReadable(true);
				folder.setWritable(true);
				folder.setExecutable(true);
			}
			System.out.println(folder.getAbsolutePath());
			String targetFileName="rec_"+System.currentTimeMillis()+subfix;
			File file=new File(folder.getPath()+"/"+targetFileName);
			if(!file.exists()){
				try {
					file.createNewFile();
					file.setReadable(true);
					file.setWritable(true);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					logger.error(e.getStackTrace());
					return "fail";
				}
			}
			
		 if(map!=null && !map.isEmpty()){
			 for(Iterator<MultipartFile> itr=map.values().iterator();itr.hasNext();){
				 MultipartFile recFile= itr.next();
				 try {
					 FileOutputStream ous=new FileOutputStream(file);
					InputStream ins=recFile.getInputStream();
					byte[] b=recFile.getBytes();
					
						ous.write(b);

					ous.flush();
					ous.close();
					ins.close();
					logger.info("录音文件生成成功："+file.getAbsolutePath());
					logger.info("录音文件生成成功："+file.getCanonicalPath());
					logger.info("录音文件生成成功："+file.getPath());
					return "{\"saved\": 1,\"file\": "+file.getCanonicalPath()+"}";
				} catch (IOException e) {
					// TODO Auto-generated catch block
					logger.error(e.getStackTrace());
					return "fail";
				}
			 }
		 }
		
		
	          
	        return "fail";
		
	}
	


	
			
}
